package cn.ibizlab.odoo.core.odoo_website.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iwebsite_page;
import cn.ibizlab.odoo.core.odoo_website.domain.Website_page;
import cn.ibizlab.odoo.core.odoo_website.filter.Website_pageSearchContext;
import cn.ibizlab.odoo.core.odoo_website.service.IWebsite_pageService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[页] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Website_pageServiceImpl implements IWebsite_pageService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iwebsite_pageClientService website_pageClientService;

    @Override
    public boolean update(Website_page website_page) {
        boolean bOk = false ;
        Iwebsite_page clientModel = this.convert2Model(website_page,null);
        website_pageClientService.update(clientModel) ;
        this.convert2Domain(clientModel,website_page) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //website_pageClientService.removeBatch(website_pages) ;

    }

    @Override
    public Website_page get(Integer id) {
        Iwebsite_page clientModel = website_pageClientService.createModel();
        clientModel.setId(id);
        website_pageClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Website_page get(Website_page website_page) {
        Website_page retwebsite_page = this.get(website_page.getId()) ;
    	BeanCopier copier=BeanCopier.create(retwebsite_page.getClass(), website_page.getClass(), false);
        copier.copy(retwebsite_page, website_page, null);
        return website_page ;
    }

    @Override
    public void updateBatch(List<Website_page> website_pages, int batchSize) {

        //website_pageClientService.updateBatch(website_pages,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iwebsite_page clientModel = website_pageClientService.createModel();
        clientModel.setId(id);
        website_pageClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Website_page website_page) {
        boolean bOk = false ;
        Iwebsite_page clientModel = this.convert2Model(website_page,null);
        website_pageClientService.create(clientModel) ;
        this.convert2Domain(clientModel,website_page);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Website_page> website_pages, int batchSize) {

                        //website_pageClientService.createBatch(website_pages,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Website_page> searchDefault(Website_pageSearchContext context) {
        List<Website_page> list = new ArrayList<Website_page>();
        Page<cn.ibizlab.odoo.core.client.model.Iwebsite_page> clientModelList = website_pageClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iwebsite_page clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Website_page>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Website_page> selectByOdoo_create(Integer id){
        Website_pageSearchContext context = new Website_pageSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Website_page> list = new ArrayList<Website_page>();
        Page<cn.ibizlab.odoo.core.client.model.Iwebsite_page> clientModelList = website_pageClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iwebsite_page clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Website_page>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Website_page> selectByOdoo_write(Integer id){
        Website_pageSearchContext context = new Website_pageSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Website_page> list = new ArrayList<Website_page>();
        Page<cn.ibizlab.odoo.core.client.model.Iwebsite_page> clientModelList = website_pageClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iwebsite_page clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Website_page>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iwebsite_page convert2Model(Website_page domain , Iwebsite_page model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = website_pageClientService.createModel() ;
        }

         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getArchDirtyFlag())
            model.setArch(domain.getArch());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getWebsite_meta_descriptionDirtyFlag())
            model.setWebsite_meta_description(domain.getWebsite_meta_description());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWebsite_urlDirtyFlag())
            model.setWebsite_url(domain.getWebsite_url());
         if(domain.getPriorityDirtyFlag())
            model.setPriority(domain.getPriority());
         if(domain.getXml_idDirtyFlag())
            model.setXml_id(domain.getXml_id());
         if(domain.getWebsite_indexedDirtyFlag())
            model.setWebsite_indexed(domain.getWebsite_indexed());
         if(domain.getView_idDirtyFlag())
            model.setView_id(domain.getView_id());
         if(domain.getTheme_template_idDirtyFlag())
            model.setTheme_template_id(domain.getTheme_template_id());
         if(domain.getWebsite_meta_og_imgDirtyFlag())
            model.setWebsite_meta_og_img(domain.getWebsite_meta_og_img());
         if(domain.getCustomize_showDirtyFlag())
            model.setCustomize_show(domain.getCustomize_show());
         if(domain.getModelDirtyFlag())
            model.setModel(domain.getModel());
         if(domain.getField_parentDirtyFlag())
            model.setField_parent(domain.getField_parent());
         if(domain.getArch_dbDirtyFlag())
            model.setArch_db(domain.getArch_db());
         if(domain.getIs_homepageDirtyFlag())
            model.setIs_homepage(domain.getIs_homepage());
         if(domain.getWebsite_publishedDirtyFlag())
            model.setWebsite_published(domain.getWebsite_published());
         if(domain.getWebsite_meta_titleDirtyFlag())
            model.setWebsite_meta_title(domain.getWebsite_meta_title());
         if(domain.getModel_data_idDirtyFlag())
            model.setModel_data_id(domain.getModel_data_id());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getModeDirtyFlag())
            model.setMode(domain.getMode());
         if(domain.getMenu_idsDirtyFlag())
            model.setMenu_ids(domain.getMenu_ids());
         if(domain.getInherit_children_idsDirtyFlag())
            model.setInherit_children_ids(domain.getInherit_children_ids());
         if(domain.getArch_baseDirtyFlag())
            model.setArch_base(domain.getArch_base());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getIs_visibleDirtyFlag())
            model.setIs_visible(domain.getIs_visible());
         if(domain.getFirst_page_idDirtyFlag())
            model.setFirst_page_id(domain.getFirst_page_id());
         if(domain.getIs_seo_optimizedDirtyFlag())
            model.setIs_seo_optimized(domain.getIs_seo_optimized());
         if(domain.getHeader_colorDirtyFlag())
            model.setHeader_color(domain.getHeader_color());
         if(domain.getIs_publishedDirtyFlag())
            model.setIs_published(domain.getIs_published());
         if(domain.getPage_idsDirtyFlag())
            model.setPage_ids(domain.getPage_ids());
         if(domain.getWebsite_idDirtyFlag())
            model.setWebsite_id(domain.getWebsite_id());
         if(domain.getUrlDirtyFlag())
            model.setUrl(domain.getUrl());
         if(domain.getHeader_overlayDirtyFlag())
            model.setHeader_overlay(domain.getHeader_overlay());
         if(domain.getWebsite_meta_keywordsDirtyFlag())
            model.setWebsite_meta_keywords(domain.getWebsite_meta_keywords());
         if(domain.getDate_publishDirtyFlag())
            model.setDate_publish(domain.getDate_publish());
         if(domain.getGroups_idDirtyFlag())
            model.setGroups_id(domain.getGroups_id());
         if(domain.getKeyDirtyFlag())
            model.setKey(domain.getKey());
         if(domain.getInherit_idDirtyFlag())
            model.setInherit_id(domain.getInherit_id());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getModel_idsDirtyFlag())
            model.setModel_ids(domain.getModel_ids());
         if(domain.getArch_fsDirtyFlag())
            model.setArch_fs(domain.getArch_fs());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Website_page convert2Domain( Iwebsite_page model ,Website_page domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Website_page();
        }

        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getArchDirtyFlag())
            domain.setArch(model.getArch());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getWebsite_meta_descriptionDirtyFlag())
            domain.setWebsite_meta_description(model.getWebsite_meta_description());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWebsite_urlDirtyFlag())
            domain.setWebsite_url(model.getWebsite_url());
        if(model.getPriorityDirtyFlag())
            domain.setPriority(model.getPriority());
        if(model.getXml_idDirtyFlag())
            domain.setXml_id(model.getXml_id());
        if(model.getWebsite_indexedDirtyFlag())
            domain.setWebsite_indexed(model.getWebsite_indexed());
        if(model.getView_idDirtyFlag())
            domain.setView_id(model.getView_id());
        if(model.getTheme_template_idDirtyFlag())
            domain.setTheme_template_id(model.getTheme_template_id());
        if(model.getWebsite_meta_og_imgDirtyFlag())
            domain.setWebsite_meta_og_img(model.getWebsite_meta_og_img());
        if(model.getCustomize_showDirtyFlag())
            domain.setCustomize_show(model.getCustomize_show());
        if(model.getModelDirtyFlag())
            domain.setModel(model.getModel());
        if(model.getField_parentDirtyFlag())
            domain.setField_parent(model.getField_parent());
        if(model.getArch_dbDirtyFlag())
            domain.setArch_db(model.getArch_db());
        if(model.getIs_homepageDirtyFlag())
            domain.setIs_homepage(model.getIs_homepage());
        if(model.getWebsite_publishedDirtyFlag())
            domain.setWebsite_published(model.getWebsite_published());
        if(model.getWebsite_meta_titleDirtyFlag())
            domain.setWebsite_meta_title(model.getWebsite_meta_title());
        if(model.getModel_data_idDirtyFlag())
            domain.setModel_data_id(model.getModel_data_id());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getModeDirtyFlag())
            domain.setMode(model.getMode());
        if(model.getMenu_idsDirtyFlag())
            domain.setMenu_ids(model.getMenu_ids());
        if(model.getInherit_children_idsDirtyFlag())
            domain.setInherit_children_ids(model.getInherit_children_ids());
        if(model.getArch_baseDirtyFlag())
            domain.setArch_base(model.getArch_base());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getIs_visibleDirtyFlag())
            domain.setIs_visible(model.getIs_visible());
        if(model.getFirst_page_idDirtyFlag())
            domain.setFirst_page_id(model.getFirst_page_id());
        if(model.getIs_seo_optimizedDirtyFlag())
            domain.setIs_seo_optimized(model.getIs_seo_optimized());
        if(model.getHeader_colorDirtyFlag())
            domain.setHeader_color(model.getHeader_color());
        if(model.getIs_publishedDirtyFlag())
            domain.setIs_published(model.getIs_published());
        if(model.getPage_idsDirtyFlag())
            domain.setPage_ids(model.getPage_ids());
        if(model.getWebsite_idDirtyFlag())
            domain.setWebsite_id(model.getWebsite_id());
        if(model.getUrlDirtyFlag())
            domain.setUrl(model.getUrl());
        if(model.getHeader_overlayDirtyFlag())
            domain.setHeader_overlay(model.getHeader_overlay());
        if(model.getWebsite_meta_keywordsDirtyFlag())
            domain.setWebsite_meta_keywords(model.getWebsite_meta_keywords());
        if(model.getDate_publishDirtyFlag())
            domain.setDate_publish(model.getDate_publish());
        if(model.getGroups_idDirtyFlag())
            domain.setGroups_id(model.getGroups_id());
        if(model.getKeyDirtyFlag())
            domain.setKey(model.getKey());
        if(model.getInherit_idDirtyFlag())
            domain.setInherit_id(model.getInherit_id());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getModel_idsDirtyFlag())
            domain.setModel_ids(model.getModel_ids());
        if(model.getArch_fsDirtyFlag())
            domain.setArch_fs(model.getArch_fs());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
